<template>
  <div class="app-container">
    <div class="filter-container">
      <div>
        <el-select v-model="listQuery.status" placeholder="订单状态" clearable style="width: 140px;" class="filter-item">
          <el-option v-for="item in statusOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <el-select v-model="listQuery.supplierChannelId" placeholder="配货货源" clearable filterable style="width: 140px; margin-left: 10px;" class="filter-item">
          <el-option v-for="item in supplierChannelOptions" :key="item.key" :label="item.display_name" :value="item.key" />
        </el-select>
        <el-input v-model="listQuery.customizedOrderId" placeholder="自定义订单号" clearable style="width: 130px; margin-left: 10px;" class="filter-item" />
        <el-input v-model="listQuery.sku" placeholder="货号" clearable style="width: 130px; margin-left: 10px;" class="filter-item" />
        <el-input v-model="listQuery.receiverName" placeholder="收件人" clearable style="width: 130px; margin-left: 10px;" class="filter-item" />
      </div>
      <div>
        <el-date-picker
          v-model="createTime"
          type="daterange"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          range-separator="-"
          start-placeholder="下单日期"
          end-placeholder="下单日期"
        />
        <el-input v-model="listQuery.expressNo" placeholder="快递单号" clearable style="width: 130px; margin-left: 10px; margin-top: 8px;" class="filter-item" />
        <el-date-picker
          v-model="feedbackTime"
          type="daterange"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          range-separator="-"
          start-placeholder="反馈日期"
          end-placeholder="反馈日期"
          style="margin-left: 10px;"
        />
      </div>
      <div>
        <el-button v-waves class="filter-item" style="margin-top: 8px;" type="primary" icon="el-icon-search" @click="handleFilter">
          查询
        </el-button>
        <el-button v-waves class="filter-item" style="margin-left: 10px; margin-top: 8px;" type="info" icon="el-icon-refresh" @click="handleRefresh">
          重置
        </el-button>
        <router-link :to="'/distributor/distribution/booking'" style="margin-left: 10px;">
          <el-button type="info" icon="el-icon-right">
            快速下单
          </el-button>
        </router-link>
        <el-button v-waves class="filter-item" style="margin-left: 10px; margin-top: 8px;" type="info" icon="el-icon-upload" @click="handleUpload">
          批量导入
        </el-button>
        <el-button v-waves class="filter-item" style="margin-left: 10px; margin-top: 8px;" type="info" icon="el-icon-download" @click="handleExport">
          导出订单
        </el-button>
        <el-button v-waves class="filter-item" style="margin-left: 10px; margin-top: 8px;" type="info" @click="handlePayAll">
          付款全部订单
        </el-button>
        <el-button v-waves class="filter-item" style="margin-left: 10px; margin-top: 8px;" type="info" @click="handlePaySelected">
          付款选中订单
        </el-button>
      </div>
    </div>

    <el-table v-loading="listLoading" :data="list" :header-cell-style="headerCellStyle" border stripe element-loading-text="拼命加载中" @selection-change="changeListCheckBoxValue">
      <el-table-column type="selection" align="center" width="50px" />
      <el-table-column label="下单账号" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.gatewayAccountName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="系统订单号" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.orderId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="自定义订单号" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.customizedOrderId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="货源" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.supplierChannelShortName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="货号" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.sku }}</span>
        </template>
      </el-table-column>
      <el-table-column label="尺码" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.normalizedSize }}</span>
        </template>
      </el-table-column>
      <el-table-column label="市场价" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.marketPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column label="折扣" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="折扣价" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.discountPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column label="付款金额" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.payPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column label="运费" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.deliveryCharge }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收件人" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.receiverName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="下单快递" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.expectDeliveryTypeName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实发快递" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.actualDeliveryTypeName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="快递单号" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.expressNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单状态" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.statusName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单日期" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="反馈日期" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.feedbackTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计发货日期" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.onShipmentDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" width="110px">
        <template slot-scope="{row}">
          <span>{{ row.remark }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNo" :limit.sync="listQuery.pageSize" @pagination="getList" />

  </div>
</template>

<script>
import { Message } from 'element-ui'
import { fetchEnum } from '@/api/system-api'
import { getOrderByPage, downloadOrder, payAllOrder, paySelectedOrder } from '@/api/distributor/distribution-api'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { toOptionsList, handleDownloadWithUtf8Filename } from '@/utils/index'

export default {
  name: 'DistributorOrder',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      payAllSuccessMsgTemplate: '成功支付订单数 {0}, 成功支付订单金额 {1}',
      paySelectedSuccessMsgTemplate: '成功支付订单数 {0}, 成功支付订单金额 {1}',
      statusOptions: [],
      supplierChannelOptions: [],
      createTime: [],
      feedbackTime: [],
      listQuery: {
        pageNo: 1,
        pageSize: 20,
        customizedOrderId: null,
        sku: null,
        supplierChannelId: null,
        receiverName: null,
        expressNo: null,
        status: null,
        createTimeBegin: null,
        createTimeEnd: null,
        feedbackTimeBegin: null,
        feedbackTimeEnd: null
      },
      exportLoading: false,

      listLoading: true,
      list: null,
      total: 0,
      listCheckBoxData: []

    }
  },
  async created() {
    await this.initLoad()
    await this.getList()
  },
  methods: {
    async initLoad() {
      const dataDictsParam = ['CUSTOMER_ORDER_STATUS', 'SUPPLIER_CHANNEL']
      const { data } = await fetchEnum(dataDictsParam)
      for (const dataDict of data) {
        if (dataDict.code === 'CUSTOMER_ORDER_STATUS') {
          if (dataDict.options) {
            this.statusOptions = toOptionsList(dataDict.options, 'code', 'name')
          }
        }
        if (dataDict.code === 'SUPPLIER_CHANNEL') {
          if (dataDict.options) {
            this.supplierChannelOptions = toOptionsList(dataDict.options, 'id', 'shortName')
          }
        }
      }
      this.initListQuery()
      if (this.$route.params) {
        this.listQuery.status = this.$route.params.status
      }
    },
    getList() {
      this.listLoading = true
      getOrderByPage(this.listQuery).then(response => {
        const { data } = response
        this.list = data.result
        this.total = data.totalCount
        setTimeout(() => {
          this.listLoading = false
        }, 1 * 1000)
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
        this.listLoading = false
      })
    },
    initListQuery() {
      this.listQuery = {
        pageNo: 1,
        pageSize: 20,
        customizedOrderId: null,
        sku: null,
        supplierChannelId: null,
        receiverName: null,
        expressNo: null,
        status: null,
        createTimeBegin: null,
        createTimeEnd: null,
        feedbackTimeBegin: null,
        feedbackTimeEnd: null
      }
    },

    handleFilter() {
      this.getList()
    },
    handleRefresh() {
      this.initListQuery()
      this.getList()
    },
    handleUpload() {
      Message({
        message: '该功能暂停使用',
        type: 'error',
        duration: 5 * 1000
      })
    },
    handleExport() {
      var param = JSON.parse(JSON.stringify(this.listQuery))
      param.pageNo = null
      param.pageSize = null
      this.exportLoading = true
      downloadOrder(param).then(response => {
        handleDownloadWithUtf8Filename(response)
        this.exportLoading = false
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
        this.exportLoading = false
      })
    },
    handlePayAll() {
      payAllOrder().then(response => {
        const { data } = response
        const payOrderCount = data.payOrderCount
        const totalPay = data.totalPay
        const msg = this.payAllSuccessMsgTemplate.format(payOrderCount, totalPay)
        Message({
          message: msg,
          type: 'success',
          duration: 5 * 1000
        })
        this.getList()
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
      })
    },
    handlePaySelected() {
      var param = {}
      paySelectedOrder(param).then(response => {
        const { data } = response
        const payOrderCount = data.payOrderCount
        const totalPay = data.totalPay
        const msg = this.paySelectedSuccessMsgTemplate.format(payOrderCount, totalPay)
        Message({
          message: msg,
          type: 'success',
          duration: 5 * 1000
        })
        this.getList()
      }).catch(error => {
        Message({
          message: error,
          type: 'error',
          duration: 5 * 1000
        })
      })
    },
    headerCellStyle({ row, rowIndex }) {
      return 'background-color: #42b983; color: #FFF;'
    },
    changeListCheckBoxValue(val) {
      console.log(val)
    }

  }
}

</script>
